<template>
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width:100%">
    <el-col>
      <span>{{ data.name }}</span>
    </el-col>
    <el-col>
      <el-row type="flex" justify="end">
        <el-col>{{ data.manager }}</el-col>
        <!-- 下拉菜单 element -->
        <el-col>
          <!-- 监听下拉菜单的点击事件 -->
          <el-dropdown @command="operateDepts">
            <span>
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu>
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <template v-if="!isRoot">
                <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                <el-dropdown-item command="del">删除部门</el-dropdown-item>
              </template>

            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>

</template>

<script>
import { delDepartments } from '@/api/departments'
export default {
  props: {
    data: {
      type: Object, // 对象类型
      required: true // 要求对方使用您的组件的时候 必须传treeNode属性 如果不传 就会报错
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    async  operateDepts(type) {
      if (type === 'add') {
        // 添加子部门的操作
        // 告诉父组件 显示弹层
        this.$emit('addDepts', this.data)
      } else if (type === 'edit') {
        this.$emit('editDepts', this.data)
      } else {
        try {
          await this.$confirm('确定要删除该部门吗')
          await delDepartments(this.data.id)
          await this.$emit('delDepts')
          this.$message.success('删除部门成功')
        } catch (error) {
          console.log(error)
        }
      }
    }
  }
}
</script>

<style>

</style>
